from tkinter import *
from tkinter import scrolledtext
from datetime import datetime
import sys
from io import StringIO


class myScrolledText(scrolledtext.ScrolledText):
    #def __init__(self, master=None):
        #scrolledtext.ScrolledText.__init__(self, master)
        #StringIO.__init__(self)
    def write(self, outStr):
        #StringIO.write(self, outStr)
        self.config(state='normal')
        if outStr in ["\n",""]:
            self.insert('end', outStr)
        else:
            self.insert(
                'end',
                datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " >>> " + outStr)
        self.config(state='disabled')
        self.see("end")
        self.master.update()
    #def flush(self):
    #    pass


class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.yournameEntry = Entry(self)
        self.yournameEntry.grid(row=0, sticky=E)
        self.helloButton = Button(self, text="Hello", command=self.click)
        self.helloButton.grid(row=0,column=1,sticky=W)
        self.consoleText = myScrolledText(self)
        self.consoleText.grid(row=1, columnspan=2)
        self.consoleText.config(state=DISABLED)
    '''def printInText(self,text):
        self.consoleText.config(state=NORMAL)
        self.consoleText.insert(END, text)
        self.consoleText.config(state=DISABLED)
        self.consoleText.see(END)
        self.update()'''
    def click(self):
        print("Hello {}!".format(self.yournameEntry.get()))
        if self.yournameEntry.get()=="q":
            raise ValueError

if __name__=="__main__":
    app=App()
    sys.stdout=app.consoleText
    sys.stderr=app.consoleText
    app.mainloop()